//
//  CTServer.m
//  Astro
//
//  Created by Cuongth on 12/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CTServer.h"


@implementation CTServer

+ (NSInteger) requestURLSignUpWithUserName:(NSString*)userName 
								  password:(NSString*)password 								  
								  birthday:(NSString*)birthday 
									gender:(NSString*)gender
								  andPhone:(NSString*)phone
{
	NSString *hash = [[NSString stringWithFormat:@"%@%@%@", userName, password, Default_Secret] md5];
	NSString *urlString = [NSString stringWithFormat:API_SignUp, hash, Default_PartnerId, userName, password, userName, birthday, gender, phone, Default_RequestSource];
	NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];

	return [result intValue];
}

+ (BOOL) requestURLSignInWithUserName:(NSString*)userName
							   andPassword:(NSString*)password
{
	NSString *hash = [[NSString stringWithFormat:@"%@%@%@", userName, password, Default_Secret] md5];
	NSString *urlString = [NSString stringWithFormat:API_Login, hash, Default_PartnerId, userName, password];
	NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];

	NSLog(@"REsult: %@", result);
	
	return [[[result JSONValue] objectForKey:@"status"] boolValue];
}


+ (SOUser*) requestURLGetUserWithUserName:(NSString*)userName{
	NSString *hash = [[NSString stringWithFormat:@"%@%@", userName, Default_Secret] md5];
	NSString *urlString = [NSString stringWithFormat:API_GetUser, hash, Default_PartnerId, userName];		
	NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
	
	NSDictionary *dict = [result JSONValue];
	NSLog(@"Dict: %@", dict);
	
	SOUser *user = [[SOUser alloc] init];
	user.hash = [dict objectForKey:@"hash"];
	user.birthday = [dict objectForKey:@"birthday"];
	user.fullName = [dict objectForKey:@"fullname"];
	user.message = [dict objectForKey:@"message"];
	user.phone = [dict objectForKey:@"phone"];
	user.name = [dict objectForKey:@"username"];

	user.sex = [[dict objectForKey:@"gender"] intValue];
	user.status = [[dict objectForKey:@"status"] intValue];
	user.numberOfXu = [[dict objectForKey:@"money"] intValue];
	user.userid = [[dict objectForKey:@"userid"] intValue];
	
	return user;
}

+ (LuckyNumberContent*) requestURLGetLuckyNumberInfo:(NSInteger)userId withCungUser:(NSInteger)cungUser {
	NSString *hash = [[NSString stringWithFormat:@"%@%d%d%@", Default_PartnerId, userId, cungUser, Default_Secret] md5];
	NSString *urlString = [NSString stringWithFormat:API_LuckyNumber, hash, Default_PartnerId, userId, cungUser];
	NSLog(@"urlString: %@", urlString);
	NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
	NSLog(@"result: %@", result);	
	NSDictionary *dict = [result JSONValue];
	NSLog(@"Dict: %@", dict);
	LuckyNumberContent *lkNum = [[LuckyNumberContent alloc] init];
	lkNum.luckyNum = [[dict objectForKey:@"luckyNumber"] intValue];
	lkNum.content = [dict objectForKey:@"message"];
	return lkNum;
}

+ (NSString*) requestURLGetDailyInfo:(NSInteger)userId withCungUser:(NSInteger)cungUser {
	NSString *hash = [[NSString stringWithFormat:@"%@%d%d%@", Default_PartnerId, userId, cungUser, Default_Secret] md5];
	NSString *urlString = [NSString stringWithFormat:API_Daily, hash, Default_PartnerId, userId, cungUser];
	NSLog(@"urlString: %@", urlString);
	NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
	NSLog(@"result: %@", result);	
	NSDictionary *dict = [result JSONValue];
	NSLog(@"Dict: %@", dict);
	
	NSString *dailyContent = [dict objectForKey:@"message"];
	return dailyContent;
}

+ (NSString*) requestURLGetGoodBadDay:(int)startDate toDate:(int)endDate withType:(NSString*)type andUserId:(int)userId andIsFree:(int)isFree {


}

+ (NSString*) requestURLGetCompareTwoAges:(int)startDate toDate:(int)endDate withType:(NSString*)type andUserId:(int)userId andIsFree:(int)isFree {

}

+ (NSString*) requestURLGetCommentary:(int)userId withRequestType:(int)type andSubtype:(int)subType andTime:(int)timeRequest andIsFree:(int)isFree {


}							 


@end



// Implement MD5 Method
@implementation NSString (MD5Extensions)
- (NSString *) md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
			@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
			result[0], result[1], result[2], result[3], 
			result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11],
			result[12], result[13], result[14], result[15]
			];  
}
@end

@implementation NSData (MD5Extensions)
- (NSString*)md5
{
    unsigned char result[16];
    CC_MD5( self.bytes, self.length, result ); // This is the md5 call
    return [NSString stringWithFormat:
			@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
			result[0], result[1], result[2], result[3], 
			result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11],
			result[12], result[13], result[14], result[15]
			];  
}
@end

